/**
 *
 */
import OpenApi;
import OpenApi.OpenApiUtil;

extends OpenApi;


init(config: OpenApiUtil.Config){
  super(config);
  @endpointRule = 'central';
  
  checkConfig(config);
  @endpoint = getEndpoint('moguan-sdk', @regionId, @endpointRule, @network, @suffix, @endpointMap, @endpoint);
}

function getEndpoint(productId: string, regionId: string, endpointRule: string, network: string, suffix: string, endpointMap: map[string]string, endpoint: string) throws: string{
  if (!$isNull(endpoint)) {
    return endpoint;
  }
  
  if (!$isNull(endpointMap) && !$isNull(endpointMap[regionId])) {
    return endpointMap[regionId];
  }
  return OpenApiUtil.getEndpointRules(productId, regionId, endpointRule, network, suffix);
}

model RegisterDeviceRequest {
  appKey?: string(name='AppKey', description='This parameter is required.', example='NWTtS623eqo6s070'),
  deviceId?: string(name='DeviceId', description='This parameter is required.', example='99daf4a623f2b623ae08e79d6d4bf686'),
  extend?: map[string]any(name='Extend', description='This parameter is required.', example='{}'),
  sdkCode?: string(name='SdkCode', description='This parameter is required.', example='SDKCodeTest01'),
  userDeviceId?: string(name='UserDeviceId', description='This parameter is required.', example='D001'),
}

model RegisterDeviceShrinkRequest {
  appKey?: string(name='AppKey', description='This parameter is required.', example='NWTtS623eqo6s070'),
  deviceId?: string(name='DeviceId', description='This parameter is required.', example='99daf4a623f2b623ae08e79d6d4bf686'),
  extendShrink?: string(name='Extend', description='This parameter is required.', example='{}'),
  sdkCode?: string(name='SdkCode', description='This parameter is required.', example='SDKCodeTest01'),
  userDeviceId?: string(name='UserDeviceId', description='This parameter is required.', example='D001'),
}

model RegisterDeviceResponseBody = {
  data?: {
    license?: string(name='License', example='rSDUqJEawcrhaHVDXgQQ2vV3eOQDzuos5TAJgx9uolqVaAKkgcBHfWd/jYknsiVeYxsLWyscP0U6ia0XL/u6t7ira9XnI3Jv9qHzosrAW09YrT68VigxqwrutxtexXGgrXFzYmcMMe05rYhEmyyoeNu0CB40HxggXIIw10vH0pvhMLd0ssz6FbaOGhZ/7WDzFAqeXlz7+whZFNlXwaCfIwHTDIj9nBHHsBzWWocOHO=='),
    publicKey?: string(name='PublicKey', example='SSTfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwdTbYqDHxAlmdSFowPthsG3wKyXdembceyc5j31FZIYGESE4x6ND0al5ejdx26d2ZMRDzlkjnLqUN3snezRA1x0qs92taGXMrIvYDi0dEsz3X/a/VXHPxNu0+/PBT9RYzakLDV9F/6QdYn4PQUvHSTfz2ghaS5SCj++VVDe4CBBIDAAPB'),
    rid?: string(name='Rid', example='1082f5e57a004a0799198d4a370c3efa'),
    signature?: string(name='Signature', example='VnxhWhjL2D3kkGcv8Q/wHzyD6dTEYIDfnIgzDWLS7iQRiCWLu1K+EA+Q6iiH1lpaDNGeQ65zVpbB1wtGMmJymQMJeJ5RHzEo74wwXP48Yfn6tdAoZwtLkxXqZo5N99W/JyEyHyeisC44ZIpLcs1YPv3Wr+uRirUgjHhZXorxJ1E='),
  }(name='Data'),
  errorCode?: int32(name='ErrorCode', example='0'),
  errorMessage?: string(name='ErrorMessage'),
  message?: string(name='Message', example='success'),
  requestId?: string(name='RequestId', example='A68E0F1E-9CEE-4BB9-8880-943730FFD9A9'),
}

model RegisterDeviceResponse = {
  headers?: map[string]string(name='headers'),
  statusCode?: int32(name='statusCode'),
  body?: RegisterDeviceResponseBody(name='body'),
}

/**
 * @summary 注册设备
 *
 * @param tmpReq RegisterDeviceRequest
 * @param runtime runtime options for this request RuntimeOptions
 * @return RegisterDeviceResponse
 */
async function registerDeviceWithOptions(tmpReq: RegisterDeviceRequest, runtime: $RuntimeOptions): RegisterDeviceResponse {
  tmpReq.validate();
  var request = new RegisterDeviceShrinkRequest{};
  OpenApiUtil.convert(tmpReq, request);
  if (!$isNull(tmpReq.extend)) {
    request.extendShrink = OpenApiUtil.arrayToStringWithSpecifiedStyle(tmpReq.extend, 'Extend', 'json');
  }
  var body : map[string]any = {};
  if (!$isNull(request.appKey)) {
    body['AppKey'] = request.appKey;
  }
  if (!$isNull(request.deviceId)) {
    body['DeviceId'] = request.deviceId;
  }
  if (!$isNull(request.extendShrink)) {
    body['Extend'] = request.extendShrink;
  }
  if (!$isNull(request.sdkCode)) {
    body['SdkCode'] = request.sdkCode;
  }
  if (!$isNull(request.userDeviceId)) {
    body['UserDeviceId'] = request.userDeviceId;
  }
  var req = new OpenApiUtil.OpenApiRequest{ 
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApiUtil.Params{
    action = 'RegisterDevice',
    version = '2021-04-15',
    protocol = 'HTTPS',
    pathname = '/',
    method = 'POST',
    authType = 'AK',
    style = 'RPC',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  if ($isNull(@signatureVersion) || @signatureVersion != 'v4') {
    return callApi(params, req, runtime);
  } else {
    return execute(params, req, runtime);
  }
}

/**
 * @summary 注册设备
 *
 * @param request RegisterDeviceRequest
 * @return RegisterDeviceResponse
 */
async function registerDevice(request: RegisterDeviceRequest): RegisterDeviceResponse {
  var runtime = new $RuntimeOptions{};
  return registerDeviceWithOptions(request, runtime);
}

